package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.mapper.SysDepartmentMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysDepartment;
import com.nineclock.system.service.SysDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 小康
 * @CreateTime: 2021-06-17 11:15
 * @Description:
 */
@Slf4j
@Service
public class SysDepartmentServiceImpl implements SysDepartmentService {
    @Autowired
    SysDepartmentMapper departmentMapper;
    /**
     *  @Date: 2021/6/17 11:33
     *  部门查询
     *  1). 获取当前登录用户的企业ID
     * 2). 查询当前企业下的顶级部门列表(parentId为0)
     * 3). 递归查询上级部门下的子部门列表数据
     * 4). 组装数据并返回
     */
    @Override
    public List<SysDepartmentDTO> queryDepartmentList() {
        //查询部门列表
        List<SysDepartmentDTO> departmentDTOList = this.queryChildrenDepartmentList(0L);

        return departmentDTOList;
    }

    //查询顶级部门的id
    @Override
    public List<Long> queryDepartmentIds() {
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getParentId, 0);
        wrapper.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());
        List<SysDepartment> departmentList = departmentMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(departmentList)){
            List<Long> ids = departmentList.stream().map(department -> {
                return department.getId();
            }).collect(Collectors.toList());
            return ids;
        }
        return new ArrayList<>();
    }

    @Autowired
    SysCompanyUserMapper companyUserMapper;

    @Override
    public List<Long> queryDepartmentsByUserId(Long companyUserId) {

        List<Long> ids = new ArrayList<>();

        if(companyUserId == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //查询该员工所在的部门id
        SysCompanyUser companyUser = companyUserMapper.selectById(companyUserId);
        if(companyUser == null){
            throw new NcException(ResponseEnum.USER_NOT_FOUND);
        }
        //获取部门id
        Long departmentId = companyUser.getDepartmentId();
        ids.add(departmentId); // 10011   10010
        //获取上级部门id
        while (true){
            SysDepartment department = departmentMapper.selectById(departmentId);
            Long parentId = department.getParentId(); //0

            if(parentId == 0){
                break;
            }
            ids.add(parentId);
            //
            departmentId = parentId; //10009
        }


        return ids;
    }



    public List<SysDepartmentDTO> queryChildrenDepartmentList(Long parentId){//0
        //0 : 查询一级部门  100009 查询该部门的子部门
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getParentId, parentId);
        wrapper.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());

        List<SysDepartment> departmentList = departmentMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(departmentList)){
            List<SysDepartmentDTO> departmentDTOList = BeanHelper.copyWithCollection(departmentList, SysDepartmentDTO.class);
            for (SysDepartmentDTO departmentDTO : departmentDTOList) {
                Long id = departmentDTO.getId();
                List<SysDepartmentDTO> departmentDTOS = this.queryChildrenDepartmentList(id);
                if(CollectionUtil.isNotEmpty(departmentDTOS)){
                    departmentDTO.setChildren(departmentDTOS);
                }else{
                    departmentDTO.setChildren(new ArrayList<>());
                }
            }

            return departmentDTOList;
        }
        return new ArrayList<>();
    }


}
